//
//  ByRecordWorkListVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/6.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByRecordWorkListVC.h"
#import "ByRecordListTitleCell.h"
#import "ByRecordListCell.h"
#import "ZZBPopView.h"
#import "ByMonthDatePicker.h"
#import "ByAccountDetailVC.h"

@interface ByRecordWorkListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZKBombView *monthDateView;
@property (nonatomic, strong) UILabel *titleLebel;
@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, strong) ByNetModel *monthlyModel;
@property (nonatomic, strong) ByNetModel *totalModel;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ByRecordWorkListVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}


- (ZKBombView *)monthDateView{
    if (_monthDateView == nil) {
        _monthDateView = [[ZKBombView alloc] init];
        _monthDateView.popMode = ZKBombPopModeBottom;
        _monthDateView.positionMode = ZKBombPositionModeBottom;
        _monthDateView.usePickerTitleForCustomContentMode = YES;
        
        ByMonthDatePicker *pick = [[ByMonthDatePicker alloc] init];
        pick.frame = CGRectMake(0, 0, SCREEN_WIDTH, 240);
        _monthDateView.contentView = pick;
        __weak typeof(self) weakSelf = self;
        _monthDateView.removeBlock = ^(){
            NSLog(@"取消");
        };
        pick.dateDidPicker = ^(NSArray *dateArray){
            NSLog(@"year = %@, month = %@", [dateArray firstObject], [dateArray lastObject]);
            weakSelf.year = [[dateArray firstObject] integerValue];
            weakSelf.month  = [[dateArray lastObject] integerValue];
        };
        _monthDateView.customTapOk = ^(){
            NSLog(@"确定");
             weakSelf.titleLebel.text = [NSString stringWithFormat:@"%ld年%ld月", weakSelf.year,weakSelf.month];
            NSString *daytime = @"";
            if (weakSelf.month < 10) {
                daytime = [NSString stringWithFormat:@"%ld0%ld", weakSelf.year,weakSelf.month];
            }else{
                daytime = [NSString stringWithFormat:@"%ld%ld", weakSelf.year,weakSelf.month];
            }
            [weakSelf recordWorkData:daytime];
            
        };
        
        
    }
    return _monthDateView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"记工清单";
    [self addLeftBackBarButtonItem];
    
    [self initViews];
    [self loadData];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    
    self.year =  components.year;
    self.month = components.month;
    
    NSString *daytime = @"";
    if (self.month < 10) {
        daytime = [NSString stringWithFormat:@"%ld0%ld", self.year,self.month];
    }else{
         daytime = [NSString stringWithFormat:@"%ld%ld", self.year,self.month];
    }
    [self recordWorkData:daytime];
    
    // Do any additional setup after loading the view.
}

//工人记工、工资清单查询接口
- (void)loadData{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.mobile = BY_USER.mobile;
    [ByCoreToolAlert showLoadind:@"查询中"];
    [ByApiRecord financeBillWorker:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        
        NSDictionary *monthlyDic = result.data[@"monthly"];
        NSDictionary *totalDic = result.data[@"total"];
        
        self.monthlyModel = [[ByNetModel alloc] initContentWithDic:monthlyDic];
        self.totalModel = [[ByNetModel alloc] initContentWithDic:totalDic];
        NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
        NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath0,indexPath1] withRowAnimation:UITableViewRowAnimationNone];
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)recordWorkData:(NSString *)daytime{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = BY_USER.uid;
    netModel.daytime = daytime;
    netModel.mobile = BY_USER.mobile;
    [ByApiRecord queryWorker:netModel success:^(ByNetBaseResult *result){
        
        [ByCoreToolAlert hideHud];
        self.dataArray = result.data;
        [self.tableView reloadData];
        
    } failure:^(ByNetBaseResult *result){
        
        [ByCoreToolAlert showFailureWithText:result.msg];
        
    }];
    
}


- (void)initViews{
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else{
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        ByRecordListTitleCell *cell = [ByRecordListTitleCell cellWithTableView:tableView];
        if (indexPath.row == 0) {
            
            cell.netModel = self.totalModel;
            cell.titleLabel.text = @"总金额";
            
        }else{
            cell.netModel = self.monthlyModel;
            cell.titleLabel.text = @"本月结余";
        }
        return cell;
        
    }else{
        
        ByRecordListCell *cell = [ByRecordListCell cellWithTableView:tableView];
        cell.dataArray = self.dataArray;
        cell.indexAtView = ^(NSInteger index){
            NSLog(@"index = %ld", index);
            ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
            ByAccountDetailVC *vc = [[ByAccountDetailVC alloc] init];
            vc.type = 0;
            vc.netModel = netModel;
            [self by_normalPush:vc hasNavi:YES];
        };
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 120;
    }else{
        
        return self.dataArray.count * 60;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
        return 40;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        return [UIView new];
        
    }else{
        
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        header.backgroundColor = BY_BACKGROUND_COLOR;
        
        UILabel *titleLebel = [[UILabel alloc] init];
        [header addSubview:titleLebel];
        [titleLebel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(header).offset(5);
            make.bottom.equalTo(header).offset(-5);
            make.right.equalTo(header).offset(-200);
            make.height.mas_equalTo(@20);
        }];
        titleLebel.text = [NSString stringWithFormat:@"%ld年%ld月",self.year,self.month];//@"2017年10月";
        titleLebel.textColor = TEXT_COLOR;
        titleLebel.font = FONT(16);
        self.titleLebel = titleLebel;
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [header addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(header).offset(-5);
            make.bottom.equalTo(titleLebel.mas_bottom).offset(0);
            make.size.mas_equalTo(CGSizeMake(80, 20));
        }];
        [button setTitle:@"其他月份" forState:UIControlStateNormal];
        [button setTitleColor:TEXT_COLOR forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = FONT(16);
        return header;
    }
    
}

- (void)btnAction:(UIButton *)btn{
    
    NSLog(@"其他月份");
    [self.monthDateView show];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
